<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 5 章 Python 装饰器 | Python 编程</title>
  <meta name="description" content="使用Python进行编程" />
  <meta name="generator" content="bookdown 0.17.2 and GitBook 2.6.7" />

  <meta property="og:title" content="第 5 章 Python 装饰器 | Python 编程" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="使用Python进行编程" />
  <meta name="github-repo" content="yangjh-xbmu/learningpython" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 5 章 Python 装饰器 | Python 编程" />
  
  <meta name="twitter:description" content="使用Python进行编程" />
  

<meta name="author" content="杨志宏" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="exception.html"/>
<link rel="next" href="module.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />











<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css\style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">LearningPython</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Python 简介</a><ul>
<li class="chapter" data-level="0.1" data-path="index.html"><a href="index.html#python-发展历史"><i class="fa fa-check"></i><b>0.1</b> Python 发展历史</a></li>
<li class="chapter" data-level="0.2" data-path="index.html"><a href="index.html#python-特点"><i class="fa fa-check"></i><b>0.2</b> Python 特点</a></li>
<li class="chapter" data-level="0.3" data-path="index.html"><a href="index.html#使用-python-的知名项目"><i class="fa fa-check"></i><b>0.3</b> 使用 Python 的知名项目</a></li>
<li class="chapter" data-level="0.4" data-path="index.html"><a href="index.html#学习资源"><i class="fa fa-check"></i><b>0.4</b> 学习资源</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="ide.html"><a href="ide.html"><i class="fa fa-check"></i><b>1</b> Python 开发环境搭建</a><ul>
<li class="chapter" data-level="1.1" data-path="ide.html"><a href="ide.html#在-windows-中安装-python-3"><i class="fa fa-check"></i><b>1.1</b> 在 Windows 中安装 Python 3</a></li>
<li class="chapter" data-level="1.2" data-path="ide.html"><a href="ide.html#在-macos-中安装-python-3"><i class="fa fa-check"></i><b>1.2</b> 在 MacOS 中安装 Python 3</a><ul>
<li class="chapter" data-level="1.2.1" data-path="ide.html"><a href="ide.html#安装-xcode"><i class="fa fa-check"></i><b>1.2.1</b> 安装 Xcode</a></li>
<li class="chapter" data-level="1.2.2" data-path="ide.html"><a href="ide.html#安装-homebrew"><i class="fa fa-check"></i><b>1.2.2</b> 安装 Homebrew</a></li>
<li class="chapter" data-level="1.2.3" data-path="ide.html"><a href="ide.html#安装-python3"><i class="fa fa-check"></i><b>1.2.3</b> 安装 Python3</a></li>
<li class="chapter" data-level="1.2.4" data-path="ide.html"><a href="ide.html#使用-python3"><i class="fa fa-check"></i><b>1.2.4</b> 使用 Python3</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="ide.html"><a href="ide.html#使用-python-虚拟环境"><i class="fa fa-check"></i><b>1.3</b> 使用 python 虚拟环境</a><ul>
<li class="chapter" data-level="1.3.1" data-path="ide.html"><a href="ide.html#为什么要用虚拟环境"><i class="fa fa-check"></i><b>1.3.1</b> 为什么要用虚拟环境</a></li>
<li class="chapter" data-level="1.3.2" data-path="ide.html"><a href="ide.html#虚拟环境的创建与使用"><i class="fa fa-check"></i><b>1.3.2</b> 虚拟环境的创建与使用</a></li>
<li class="chapter" data-level="1.3.3" data-path="ide.html"><a href="ide.html#使用-pipenv-管理虚拟环境"><i class="fa fa-check"></i><b>1.3.3</b> 使用 pipenv 管理虚拟环境</a></li>
<li class="chapter" data-level="1.3.4" data-path="ide.html"><a href="ide.html#虚拟环境与系统环境的区别"><i class="fa fa-check"></i><b>1.3.4</b> 虚拟环境与系统环境的区别</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="ide.html"><a href="ide.html#编辑器"><i class="fa fa-check"></i><b>1.4</b> 编辑器</a><ul>
<li class="chapter" data-level="1.4.1" data-path="ide.html"><a href="ide.html#visual-studio-code-中的必要设置"><i class="fa fa-check"></i><b>1.4.1</b> Visual Studio Code 中的必要设置</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>I 核心语法</b></span></li>
<li class="chapter" data-level="2" data-path="systax.html"><a href="systax.html"><i class="fa fa-check"></i><b>2</b> 核心语法</a><ul>
<li class="chapter" data-level="2.1" data-path="systax.html"><a href="systax.html#注释"><i class="fa fa-check"></i><b>2.1</b> 注释</a></li>
<li class="chapter" data-level="2.2" data-path="systax.html"><a href="systax.html#变量"><i class="fa fa-check"></i><b>2.2</b> 变量</a><ul>
<li class="chapter" data-level="2.2.1" data-path="systax.html"><a href="systax.html#变量名称"><i class="fa fa-check"></i><b>2.2.1</b> 变量名称</a></li>
<li class="chapter" data-level="2.2.2" data-path="systax.html"><a href="systax.html#变量赋值"><i class="fa fa-check"></i><b>2.2.2</b> 变量赋值</a></li>
<li class="chapter" data-level="2.2.3" data-path="systax.html"><a href="systax.html#同步赋值"><i class="fa fa-check"></i><b>2.2.3</b> 同步赋值</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="systax.html"><a href="systax.html#数字类型"><i class="fa fa-check"></i><b>2.3</b> 数字类型</a><ul>
<li class="chapter" data-level="2.3.1" data-path="systax.html"><a href="systax.html#查看变量类型"><i class="fa fa-check"></i><b>2.3.1</b> 查看变量类型</a></li>
<li class="chapter" data-level="2.3.2" data-path="systax.html"><a href="systax.html#整型"><i class="fa fa-check"></i><b>2.3.2</b> 整型</a></li>
<li class="chapter" data-level="2.3.3" data-path="systax.html"><a href="systax.html#浮点类型"><i class="fa fa-check"></i><b>2.3.3</b> 浮点类型</a></li>
<li class="chapter" data-level="2.3.4" data-path="systax.html"><a href="systax.html#复数"><i class="fa fa-check"></i><b>2.3.4</b> 复数</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="systax.html"><a href="systax.html#运算符"><i class="fa fa-check"></i><b>2.4</b> 运算符</a><ul>
<li class="chapter" data-level="2.4.1" data-path="systax.html"><a href="systax.html#运算符的优先级别"><i class="fa fa-check"></i><b>2.4.1</b> 运算符的优先级别</a></li>
<li class="chapter" data-level="2.4.2" data-path="systax.html"><a href="systax.html#增强赋值运算符"><i class="fa fa-check"></i><b>2.4.2</b> 增强赋值运算符</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="systax.html"><a href="systax.html#序列"><i class="fa fa-check"></i><b>2.5</b> 序列</a><ul>
<li class="chapter" data-level="2.5.1" data-path="systax.html"><a href="systax.html#索引"><i class="fa fa-check"></i><b>2.5.1</b> 索引</a></li>
<li class="chapter" data-level="2.5.2" data-path="systax.html"><a href="systax.html#分片"><i class="fa fa-check"></i><b>2.5.2</b> 分片</a></li>
<li class="chapter" data-level="2.5.3" data-path="systax.html"><a href="systax.html#序列相加"><i class="fa fa-check"></i><b>2.5.3</b> 序列相加</a></li>
<li class="chapter" data-level="2.5.4" data-path="systax.html"><a href="systax.html#序列相乘"><i class="fa fa-check"></i><b>2.5.4</b> 序列相乘</a></li>
<li class="chapter" data-level="2.5.5" data-path="systax.html"><a href="systax.html#成员资格"><i class="fa fa-check"></i><b>2.5.5</b> 成员资格</a></li>
<li class="chapter" data-level="2.5.6" data-path="systax.html"><a href="systax.html#长度最小值最大值"><i class="fa fa-check"></i><b>2.5.6</b> 长度、最小值、最大值</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="systax.html"><a href="systax.html#字符串"><i class="fa fa-check"></i><b>2.6</b> 字符串</a><ul>
<li class="chapter" data-level="2.6.1" data-path="systax.html"><a href="systax.html#创建字符串"><i class="fa fa-check"></i><b>2.6.1</b> 创建字符串</a></li>
<li class="chapter" data-level="2.6.2" data-path="systax.html"><a href="systax.html#字符串的不可变性"><i class="fa fa-check"></i><b>2.6.2</b> 字符串的不可变性</a></li>
<li class="chapter" data-level="2.6.3" data-path="systax.html"><a href="systax.html#字符串操作"><i class="fa fa-check"></i><b>2.6.3</b> 字符串操作</a></li>
<li class="chapter" data-level="2.6.4" data-path="systax.html"><a href="systax.html#字符串分片"><i class="fa fa-check"></i><b>2.6.4</b> 字符串分片</a></li>
<li class="chapter" data-level="2.6.5" data-path="systax.html"><a href="systax.html#in-和-not-in-操作符"><i class="fa fa-check"></i><b>2.6.5</b> in 和 not in 操作符</a></li>
<li class="chapter" data-level="2.6.6" data-path="systax.html"><a href="systax.html#string-对象的方法"><i class="fa fa-check"></i><b>2.6.6</b> String 对象的方法</a></li>
<li class="chapter" data-level="2.6.7" data-path="systax.html"><a href="systax.html#比较字符串"><i class="fa fa-check"></i><b>2.6.7</b> 比较字符串</a></li>
<li class="chapter" data-level="2.6.8" data-path="systax.html"><a href="systax.html#遍历字符串"><i class="fa fa-check"></i><b>2.6.8</b> 遍历字符串</a></li>
<li class="chapter" data-level="2.6.9" data-path="systax.html"><a href="systax.html#字符串内容检验"><i class="fa fa-check"></i><b>2.6.9</b> 字符串内容检验</a></li>
<li class="chapter" data-level="2.6.10" data-path="systax.html"><a href="systax.html#在字符串内查找和替换"><i class="fa fa-check"></i><b>2.6.10</b> 在字符串内查找和替换</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="systax.html"><a href="systax.html#列表"><i class="fa fa-check"></i><b>2.7</b> 列表</a><ul>
<li class="chapter" data-level="2.7.1" data-path="systax.html"><a href="systax.html#列表赋值"><i class="fa fa-check"></i><b>2.7.1</b> 列表赋值</a></li>
<li class="chapter" data-level="2.7.2" data-path="systax.html"><a href="systax.html#删除元素"><i class="fa fa-check"></i><b>2.7.2</b> 删除元素</a></li>
<li class="chapter" data-level="2.7.3" data-path="systax.html"><a href="systax.html#分片赋值"><i class="fa fa-check"></i><b>2.7.3</b> 分片赋值</a></li>
<li class="chapter" data-level="2.7.4" data-path="systax.html"><a href="systax.html#列表对象常用内置方法"><i class="fa fa-check"></i><b>2.7.4</b> 列表对象常用内置方法</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="systax.html"><a href="systax.html#字典"><i class="fa fa-check"></i><b>2.8</b> 字典</a><ul>
<li class="chapter" data-level="2.8.1" data-path="systax.html"><a href="systax.html#创建字典"><i class="fa fa-check"></i><b>2.8.1</b> 创建字典</a></li>
<li class="chapter" data-level="2.8.2" data-path="systax.html"><a href="systax.html#获取修改和添加字典元素"><i class="fa fa-check"></i><b>2.8.2</b> 获取、修改和添加字典元素</a></li>
<li class="chapter" data-level="2.8.3" data-path="systax.html"><a href="systax.html#遍历字典"><i class="fa fa-check"></i><b>2.8.3</b> 遍历字典</a></li>
<li class="chapter" data-level="2.8.4" data-path="systax.html"><a href="systax.html#字典常用方法"><i class="fa fa-check"></i><b>2.8.4</b> 字典常用方法</a></li>
<li class="chapter" data-level="2.8.5" data-path="systax.html"><a href="systax.html#字典的排序"><i class="fa fa-check"></i><b>2.8.5</b> 字典的排序</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="systax.html"><a href="systax.html#元组"><i class="fa fa-check"></i><b>2.9</b> 元组</a><ul>
<li class="chapter" data-level="2.9.1" data-path="systax.html"><a href="systax.html#创建元组"><i class="fa fa-check"></i><b>2.9.1</b> 创建元组</a></li>
<li class="chapter" data-level="2.9.2" data-path="systax.html"><a href="systax.html#元组相关方法"><i class="fa fa-check"></i><b>2.9.2</b> 元组相关方法</a></li>
</ul></li>
<li class="chapter" data-level="2.10" data-path="systax.html"><a href="systax.html#控制声明"><i class="fa fa-check"></i><b>2.10</b> 控制声明</a><ul>
<li class="chapter" data-level="2.10.1" data-path="systax.html"><a href="systax.html#分支判断"><i class="fa fa-check"></i><b>2.10.1</b> 分支判断</a></li>
<li class="chapter" data-level="2.10.2" data-path="systax.html"><a href="systax.html#分支嵌套"><i class="fa fa-check"></i><b>2.10.2</b> 分支嵌套</a></li>
<li class="chapter" data-level="2.10.3" data-path="systax.html"><a href="systax.html#三元运算符"><i class="fa fa-check"></i><b>2.10.3</b> 三元运算符</a></li>
</ul></li>
<li class="chapter" data-level="2.11" data-path="systax.html"><a href="systax.html#循环"><i class="fa fa-check"></i><b>2.11</b> 循环</a><ul>
<li class="chapter" data-level="2.11.1" data-path="systax.html"><a href="systax.html#for-循环"><i class="fa fa-check"></i><b>2.11.1</b> for 循环</a></li>
<li class="chapter" data-level="2.11.2" data-path="systax.html"><a href="systax.html#范围循环"><i class="fa fa-check"></i><b>2.11.2</b> 范围循环</a></li>
<li class="chapter" data-level="2.11.3" data-path="systax.html"><a href="systax.html#while-循环"><i class="fa fa-check"></i><b>2.11.3</b> while 循环</a></li>
<li class="chapter" data-level="2.11.4" data-path="systax.html"><a href="systax.html#中断循环"><i class="fa fa-check"></i><b>2.11.4</b> 中断循环</a></li>
<li class="chapter" data-level="2.11.5" data-path="systax.html"><a href="systax.html#继续循环"><i class="fa fa-check"></i><b>2.11.5</b> 继续循环</a></li>
</ul></li>
<li class="chapter" data-level="2.12" data-path="systax.html"><a href="systax.html#函数"><i class="fa fa-check"></i><b>2.12</b> 函数</a><ul>
<li class="chapter" data-level="2.12.1" data-path="systax.html"><a href="systax.html#创建函数"><i class="fa fa-check"></i><b>2.12.1</b> 创建函数</a></li>
<li class="chapter" data-level="2.12.2" data-path="systax.html"><a href="systax.html#函数返回值"><i class="fa fa-check"></i><b>2.12.2</b> 函数返回值</a></li>
<li class="chapter" data-level="2.12.3" data-path="systax.html"><a href="systax.html#全局变量和局域变量"><i class="fa fa-check"></i><b>2.12.3</b> 全局变量和局域变量</a></li>
<li class="chapter" data-level="2.12.4" data-path="systax.html"><a href="systax.html#参数的默认值"><i class="fa fa-check"></i><b>2.12.4</b> 参数的默认值</a></li>
<li class="chapter" data-level="2.12.5" data-path="systax.html"><a href="systax.html#关键字参数"><i class="fa fa-check"></i><b>2.12.5</b> 关键字参数</a></li>
<li class="chapter" data-level="2.12.6" data-path="systax.html"><a href="systax.html#返回多个值"><i class="fa fa-check"></i><b>2.12.6</b> 返回多个值</a></li>
<li class="chapter" data-level="2.12.7" data-path="systax.html"><a href="systax.html#函数文档字符串"><i class="fa fa-check"></i><b>2.12.7</b> 函数文档字符串</a></li>
<li class="chapter" data-level="2.12.8" data-path="systax.html"><a href="systax.html#lambda表达式"><i class="fa fa-check"></i><b>2.12.8</b> lambda表达式</a></li>
<li class="chapter" data-level="2.12.9" data-path="systax.html"><a href="systax.html#args-和-kwargs"><i class="fa fa-check"></i><b>2.12.9</b> <code>*args</code> 和 <code>**kwargs</code></a></li>
<li class="chapter" data-level="2.12.10" data-path="systax.html"><a href="systax.html#参考资料"><i class="fa fa-check"></i><b>2.12.10</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="oop.html"><a href="oop.html"><i class="fa fa-check"></i><b>3</b> Python中的面向对象编程</a><ul>
<li class="chapter" data-level="3.1" data-path="oop.html"><a href="oop.html#python对象和类"><i class="fa fa-check"></i><b>3.1</b> Python对象和类</a><ul>
<li class="chapter" data-level="3.1.1" data-path="oop.html"><a href="oop.html#创建类"><i class="fa fa-check"></i><b>3.1.1</b> 创建类</a></li>
<li class="chapter" data-level="3.1.2" data-path="oop.html"><a href="oop.html#从类中创建对象"><i class="fa fa-check"></i><b>3.1.2</b> 从类中创建对象</a></li>
<li class="chapter" data-level="3.1.3" data-path="oop.html"><a href="oop.html#隐藏数据字段"><i class="fa fa-check"></i><b>3.1.3</b> 隐藏数据字段</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="oop.html"><a href="oop.html#操作符重载"><i class="fa fa-check"></i><b>3.2</b> 操作符重载</a></li>
<li class="chapter" data-level="3.3" data-path="oop.html"><a href="oop.html#继承和多态"><i class="fa fa-check"></i><b>3.3</b> 继承和多态</a><ul>
<li class="chapter" data-level="3.3.1" data-path="oop.html"><a href="oop.html#多重继承"><i class="fa fa-check"></i><b>3.3.1</b> 多重继承</a></li>
<li class="chapter" data-level="3.3.2" data-path="oop.html"><a href="oop.html#重写方法"><i class="fa fa-check"></i><b>3.3.2</b> 重写方法</a></li>
<li class="chapter" data-level="3.3.3" data-path="oop.html"><a href="oop.html#判断对象是否属于某类"><i class="fa fa-check"></i><b>3.3.3</b> 判断对象是否属于某类</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="exception.html"><a href="exception.html"><i class="fa fa-check"></i><b>4</b> 异常处理</a><ul>
<li class="chapter" data-level="4.1" data-path="exception.html"><a href="exception.html#捕获异常"><i class="fa fa-check"></i><b>4.1</b> 捕获异常</a><ul>
<li class="chapter" data-level="4.1.1" data-path="exception.html"><a href="exception.html#try-except"><i class="fa fa-check"></i><b>4.1.1</b> try-except</a></li>
<li class="chapter" data-level="4.1.2" data-path="exception.html"><a href="exception.html#多个except"><i class="fa fa-check"></i><b>4.1.2</b> 多个except</a></li>
<li class="chapter" data-level="4.1.3" data-path="exception.html"><a href="exception.html#自定义异常"><i class="fa fa-check"></i><b>4.1.3</b> 自定义异常</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="decorator.html"><a href="decorator.html"><i class="fa fa-check"></i><b>5</b> Python 装饰器</a><ul>
<li class="chapter" data-level="5.1" data-path="decorator.html"><a href="decorator.html#为什么要用装饰器"><i class="fa fa-check"></i><b>5.1</b> 为什么要用装饰器</a></li>
<li class="chapter" data-level="5.2" data-path="decorator.html"><a href="decorator.html#简单装饰器"><i class="fa fa-check"></i><b>5.2</b> 简单装饰器</a></li>
<li class="chapter" data-level="5.3" data-path="decorator.html"><a href="decorator.html#语法糖"><i class="fa fa-check"></i><b>5.3</b> @ 语法糖</a></li>
<li class="chapter" data-level="5.4" data-path="decorator.html"><a href="decorator.html#argskwargs"><i class="fa fa-check"></i><b>5.4</b> <code>*args</code>、<code>**kwargs</code></a></li>
<li class="chapter" data-level="5.5" data-path="decorator.html"><a href="decorator.html#带参数的装饰器"><i class="fa fa-check"></i><b>5.5</b> 带参数的装饰器</a></li>
<li class="chapter" data-level="5.6" data-path="decorator.html"><a href="decorator.html#类装饰器"><i class="fa fa-check"></i><b>5.6</b> 类装饰器</a></li>
<li class="chapter" data-level="5.7" data-path="decorator.html"><a href="decorator.html#functools.wraps"><i class="fa fa-check"></i><b>5.7</b> functools.wraps</a></li>
<li class="chapter" data-level="5.8" data-path="decorator.html"><a href="decorator.html#装饰器顺序"><i class="fa fa-check"></i><b>5.8</b> 装饰器顺序</a></li>
<li class="chapter" data-level="" data-path="decorator.html"><a href="decorator.html#参考资料-1"><i class="fa fa-check"></i>参考资料</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="module.html"><a href="module.html"><i class="fa fa-check"></i><b>6</b> 模块</a><ul>
<li class="chapter" data-level="6.1" data-path="module.html"><a href="module.html#创建模块"><i class="fa fa-check"></i><b>6.1</b> 创建模块</a></li>
<li class="chapter" data-level="6.2" data-path="module.html"><a href="module.html#使用模块中的指定内容"><i class="fa fa-check"></i><b>6.2</b> 使用模块中的指定内容</a></li>
<li class="chapter" data-level="6.3" data-path="module.html"><a href="module.html#dir函数"><i class="fa fa-check"></i><b>6.3</b> dir函数</a></li>
<li class="chapter" data-level="6.4" data-path="module.html"><a href="module.html#包"><i class="fa fa-check"></i><b>6.4</b> 包</a></li>
</ul></li>
<li class="part"><span><b>II 进阶</b></span></li>
<li class="chapter" data-level="7" data-path="buildinfunctions.html"><a href="buildinfunctions.html"><i class="fa fa-check"></i><b>7</b> 内置函数</a><ul>
<li class="chapter" data-level="7.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#dict函数"><i class="fa fa-check"></i><b>7.1</b> dict函数</a><ul>
<li class="chapter" data-level="7.1.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述"><i class="fa fa-check"></i><b>7.1.1</b> 描述</a></li>
<li class="chapter" data-level="7.1.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法"><i class="fa fa-check"></i><b>7.1.2</b> 语法</a></li>
<li class="chapter" data-level="7.1.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值"><i class="fa fa-check"></i><b>7.1.3</b> 返回值</a></li>
<li class="chapter" data-level="7.1.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例"><i class="fa fa-check"></i><b>7.1.4</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#zip函数"><i class="fa fa-check"></i><b>7.2</b> zip函数</a><ul>
<li class="chapter" data-level="7.2.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-1"><i class="fa fa-check"></i><b>7.2.1</b> 描述</a></li>
<li class="chapter" data-level="7.2.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-1"><i class="fa fa-check"></i><b>7.2.2</b> 语法</a></li>
<li class="chapter" data-level="7.2.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-1"><i class="fa fa-check"></i><b>7.2.3</b> 返回值</a></li>
<li class="chapter" data-level="7.2.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#特殊用法"><i class="fa fa-check"></i><b>7.2.4</b> 特殊用法</a></li>
<li class="chapter" data-level="7.2.5" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-1"><i class="fa fa-check"></i><b>7.2.5</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#list函数"><i class="fa fa-check"></i><b>7.3</b> list函数</a><ul>
<li class="chapter" data-level="7.3.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-2"><i class="fa fa-check"></i><b>7.3.1</b> 描述</a></li>
<li class="chapter" data-level="7.3.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-2"><i class="fa fa-check"></i><b>7.3.2</b> 语法</a></li>
<li class="chapter" data-level="7.3.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-2"><i class="fa fa-check"></i><b>7.3.3</b> 返回值</a></li>
<li class="chapter" data-level="7.3.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-2"><i class="fa fa-check"></i><b>7.3.4</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#min函数"><i class="fa fa-check"></i><b>7.4</b> min函数</a><ul>
<li class="chapter" data-level="7.4.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-3"><i class="fa fa-check"></i><b>7.4.1</b> 描述</a></li>
<li class="chapter" data-level="7.4.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-3"><i class="fa fa-check"></i><b>7.4.2</b> 语法</a></li>
<li class="chapter" data-level="7.4.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-3"><i class="fa fa-check"></i><b>7.4.3</b> 返回值</a></li>
<li class="chapter" data-level="7.4.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-3"><i class="fa fa-check"></i><b>7.4.4</b> 实例</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="iterator.html"><a href="iterator.html"><i class="fa fa-check"></i><b>8</b> Python可迭代对象</a><ul>
<li class="chapter" data-level="8.1" data-path="iterator.html"><a href="iterator.html#可迭代对象"><i class="fa fa-check"></i><b>8.1</b> 可迭代对象</a></li>
<li class="chapter" data-level="8.2" data-path="iterator.html"><a href="iterator.html#迭代器"><i class="fa fa-check"></i><b>8.2</b> 迭代器</a></li>
<li class="chapter" data-level="8.3" data-path="iterator.html"><a href="iterator.html#生成器"><i class="fa fa-check"></i><b>8.3</b> 生成器</a></li>
<li class="chapter" data-level="8.4" data-path="iterator.html"><a href="iterator.html#三者之间关系"><i class="fa fa-check"></i><b>8.4</b> 三者之间关系</a></li>
<li class="chapter" data-level="8.5" data-path="iterator.html"><a href="iterator.html#迭代器长度的计算"><i class="fa fa-check"></i><b>8.5</b> 迭代器长度的计算</a></li>
<li class="chapter" data-level="8.6" data-path="iterator.html"><a href="iterator.html#yield"><i class="fa fa-check"></i><b>8.6</b> yield</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="underline.html"><a href="underline.html"><i class="fa fa-check"></i><b>9</b> 单、双下划线的区别</a><ul>
<li class="chapter" data-level="9.1" data-path="underline.html"><a href="underline.html#单下划线开头"><i class="fa fa-check"></i><b>9.1</b> 单下划线开头</a></li>
<li class="chapter" data-level="9.2" data-path="underline.html"><a href="underline.html#双下划线开头"><i class="fa fa-check"></i><b>9.2</b> 双下划线开头</a></li>
<li class="chapter" data-level="9.3" data-path="underline.html"><a href="underline.html#双下划线开头和结尾"><i class="fa fa-check"></i><b>9.3</b> 双下划线开头和结尾</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="re.html"><a href="re.html"><i class="fa fa-check"></i><b>10</b> 正则表达式</a><ul>
<li class="chapter" data-level="10.1" data-path="re.html"><a href="re.html#正则表达式发展简史"><i class="fa fa-check"></i><b>10.1</b> 正则表达式发展简史</a></li>
<li class="chapter" data-level="10.2" data-path="re.html"><a href="re.html#python中使用正则表达式的流程"><i class="fa fa-check"></i><b>10.2</b> Python中使用正则表达式的流程</a></li>
<li class="chapter" data-level="10.3" data-path="re.html"><a href="re.html#正则表达式的构成要素"><i class="fa fa-check"></i><b>10.3</b> 正则表达式的构成要素</a><ul>
<li class="chapter" data-level="10.3.1" data-path="re.html"><a href="re.html#定界符"><i class="fa fa-check"></i><b>10.3.1</b> 定界符</a></li>
<li class="chapter" data-level="10.3.2" data-path="re.html"><a href="re.html#原子"><i class="fa fa-check"></i><b>10.3.2</b> 原子</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="re.html"><a href="re.html#元字符"><i class="fa fa-check"></i><b>10.4</b> 元字符</a><ul>
<li class="chapter" data-level="10.4.1" data-path="re.html"><a href="re.html#匹配单个字符的元字符"><i class="fa fa-check"></i><b>10.4.1</b> 匹配单个字符的元字符</a></li>
<li class="chapter" data-level="10.4.2" data-path="re.html"><a href="re.html#常用转义符"><i class="fa fa-check"></i><b>10.4.2</b> 常用转义符</a></li>
<li class="chapter" data-level="10.4.3" data-path="re.html"><a href="re.html#提供计数功能的元字符"><i class="fa fa-check"></i><b>10.4.3</b> 提供计数功能的元字符</a></li>
<li class="chapter" data-level="10.4.4" data-path="re.html"><a href="re.html#匹配位置的元字符"><i class="fa fa-check"></i><b>10.4.4</b> 匹配位置的元字符</a></li>
<li class="chapter" data-level="10.4.5" data-path="re.html"><a href="re.html#其他元字符"><i class="fa fa-check"></i><b>10.4.5</b> 其他元字符</a></li>
</ul></li>
<li class="chapter" data-level="10.5" data-path="re.html"><a href="re.html#模式修饰符"><i class="fa fa-check"></i><b>10.5</b> 模式修饰符</a></li>
<li class="chapter" data-level="10.6" data-path="re.html"><a href="re.html#在python中使用正则表达式"><i class="fa fa-check"></i><b>10.6</b> 在Python中使用正则表达式</a><ul>
<li class="chapter" data-level="10.6.1" data-path="re.html"><a href="re.html#search"><i class="fa fa-check"></i><b>10.6.1</b> search()</a></li>
<li class="chapter" data-level="10.6.2" data-path="re.html"><a href="re.html#compile"><i class="fa fa-check"></i><b>10.6.2</b> compile()</a></li>
<li class="chapter" data-level="10.6.3" data-path="re.html"><a href="re.html#findall"><i class="fa fa-check"></i><b>10.6.3</b> findall()</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="argparse.html"><a href="argparse.html"><i class="fa fa-check"></i><b>11</b> 命令行参数模块argparse</a><ul>
<li class="chapter" data-level="11.1" data-path="argparse.html"><a href="argparse.html#创建解析器"><i class="fa fa-check"></i><b>11.1</b> 创建解析器</a></li>
<li class="chapter" data-level="11.2" data-path="argparse.html"><a href="argparse.html#添加参数选项"><i class="fa fa-check"></i><b>11.2</b> 添加参数选项</a><ul>
<li class="chapter" data-level="11.2.1" data-path="argparse.html"><a href="argparse.html#name-or-flags"><i class="fa fa-check"></i><b>11.2.1</b> name or flags</a></li>
<li class="chapter" data-level="11.2.2" data-path="argparse.html"><a href="argparse.html#help"><i class="fa fa-check"></i><b>11.2.2</b> help</a></li>
<li class="chapter" data-level="11.2.3" data-path="argparse.html"><a href="argparse.html#default和type"><i class="fa fa-check"></i><b>11.2.3</b> default和type</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="argparse.html"><a href="argparse.html#参数解析"><i class="fa fa-check"></i><b>11.3</b> 参数解析</a></li>
<li class="chapter" data-level="11.4" data-path="argparse.html"><a href="argparse.html#小结"><i class="fa fa-check"></i><b>11.4</b> 小结</a></li>
<li class="chapter" data-level="11.5" data-path="argparse.html"><a href="argparse.html#参考文献"><i class="fa fa-check"></i><b>11.5</b> 参考文献</a></li>
</ul></li>
<li class="part"><span><b>III 数据抓取</b></span></li>
<li class="chapter" data-level="12" data-path="spider.html"><a href="spider.html"><i class="fa fa-check"></i><b>12</b> 网络爬虫</a><ul>
<li class="chapter" data-level="12.1" data-path="spider.html"><a href="spider.html#网络爬虫原理"><i class="fa fa-check"></i><b>12.1</b> 网络爬虫原理</a></li>
<li class="chapter" data-level="12.2" data-path="spider.html"><a href="spider.html#http请求原理"><i class="fa fa-check"></i><b>12.2</b> HTTP请求原理</a><ul>
<li class="chapter" data-level="12.2.1" data-path="spider.html"><a href="spider.html#url"><i class="fa fa-check"></i><b>12.2.1</b> URL</a></li>
<li class="chapter" data-level="12.2.2" data-path="spider.html"><a href="spider.html#请求方式"><i class="fa fa-check"></i><b>12.2.2</b> 请求方式</a></li>
<li class="chapter" data-level="12.2.3" data-path="spider.html"><a href="spider.html#requests包"><i class="fa fa-check"></i><b>12.2.3</b> requests包</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="spider.html"><a href="spider.html#编码"><i class="fa fa-check"></i><b>12.3</b> 编码</a><ul>
<li class="chapter" data-level="12.3.1" data-path="spider.html"><a href="spider.html#编码方式"><i class="fa fa-check"></i><b>12.3.1</b> 编码方式</a></li>
<li class="chapter" data-level="12.3.2" data-path="spider.html"><a href="spider.html#编码转换"><i class="fa fa-check"></i><b>12.3.2</b> 编码转换</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="spider.html"><a href="spider.html#存储"><i class="fa fa-check"></i><b>12.4</b> 存储</a><ul>
<li class="chapter" data-level="12.4.1" data-path="spider.html"><a href="spider.html#存储到文件"><i class="fa fa-check"></i><b>12.4.1</b> 存储到文件</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="13" data-path="httplib.html"><a href="httplib.html"><i class="fa fa-check"></i><b>13</b> HTTP库</a><ul>
<li class="chapter" data-level="13.1" data-path="httplib.html"><a href="httplib.html#urllib"><i class="fa fa-check"></i><b>13.1</b> urllib</a><ul>
<li class="chapter" data-level="13.1.1" data-path="httplib.html"><a href="httplib.html#发送请求"><i class="fa fa-check"></i><b>13.1.1</b> 发送请求</a></li>
<li class="chapter" data-level="13.1.2" data-path="httplib.html"><a href="httplib.html#处理异常"><i class="fa fa-check"></i><b>13.1.2</b> 处理异常</a></li>
<li class="chapter" data-level="13.1.3" data-path="httplib.html"><a href="httplib.html#解析链接"><i class="fa fa-check"></i><b>13.1.3</b> 解析链接</a></li>
</ul></li>
<li class="chapter" data-level="13.2" data-path="httplib.html"><a href="httplib.html#使用requests"><i class="fa fa-check"></i><b>13.2</b> 使用requests</a><ul>
<li class="chapter" data-level="13.2.1" data-path="httplib.html"><a href="httplib.html#安装requests"><i class="fa fa-check"></i><b>13.2.1</b> 安装requests</a></li>
<li class="chapter" data-level="13.2.2" data-path="httplib.html"><a href="httplib.html#发起请求"><i class="fa fa-check"></i><b>13.2.2</b> 发起请求</a></li>
</ul></li>
<li class="chapter" data-level="13.3" data-path="httplib.html"><a href="httplib.html#网页编码检测及转换"><i class="fa fa-check"></i><b>13.3</b> 网页编码检测及转换</a></li>
<li class="chapter" data-level="13.4" data-path="httplib.html"><a href="httplib.html#参考资料-2"><i class="fa fa-check"></i><b>13.4</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="pyquery.html"><a href="pyquery.html"><i class="fa fa-check"></i><b>14</b> Pyquery</a><ul>
<li class="chapter" data-level="14.1" data-path="pyquery.html"><a href="pyquery.html#安装"><i class="fa fa-check"></i><b>14.1</b> 安装</a></li>
<li class="chapter" data-level="14.2" data-path="pyquery.html"><a href="pyquery.html#初始化"><i class="fa fa-check"></i><b>14.2</b> 初始化</a></li>
<li class="chapter" data-level="14.3" data-path="pyquery.html"><a href="pyquery.html#获取信息"><i class="fa fa-check"></i><b>14.3</b> 获取信息</a><ul>
<li class="chapter" data-level="14.3.1" data-path="pyquery.html"><a href="pyquery.html#通过选择符选定元素"><i class="fa fa-check"></i><b>14.3.1</b> 通过选择符选定元素</a></li>
<li class="chapter" data-level="14.3.2" data-path="pyquery.html"><a href="pyquery.html#通过迭代获取最终结果"><i class="fa fa-check"></i><b>14.3.2</b> 通过迭代获取最终结果</a></li>
</ul></li>
<li class="chapter" data-level="14.4" data-path="pyquery.html"><a href="pyquery.html#参考资料-3"><i class="fa fa-check"></i><b>14.4</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="15" data-path="databases.html"><a href="databases.html"><i class="fa fa-check"></i><b>15</b> 数据存储</a><ul>
<li class="chapter" data-level="15.1" data-path="databases.html"><a href="databases.html#mysql的存储"><i class="fa fa-check"></i><b>15.1</b> MySQL的存储</a><ul>
<li class="chapter" data-level="15.1.1" data-path="databases.html"><a href="databases.html#安装pymysql和mysql"><i class="fa fa-check"></i><b>15.1.1</b> 安装PyMySQL和MySQL</a></li>
<li class="chapter" data-level="15.1.2" data-path="databases.html"><a href="databases.html#连接数据库"><i class="fa fa-check"></i><b>15.1.2</b> 连接数据库</a></li>
<li class="chapter" data-level="15.1.3" data-path="databases.html"><a href="databases.html#对数据库进行操作"><i class="fa fa-check"></i><b>15.1.3</b> 对数据库进行操作</a></li>
</ul></li>
<li class="chapter" data-level="15.2" data-path="databases.html"><a href="databases.html#mongodb"><i class="fa fa-check"></i><b>15.2</b> MongoDB</a><ul>
<li class="chapter" data-level="15.2.1" data-path="databases.html"><a href="databases.html#在macos中的安装"><i class="fa fa-check"></i><b>15.2.1</b> 在macOS中的安装</a></li>
<li class="chapter" data-level="15.2.2" data-path="databases.html"><a href="databases.html#连接mongodb"><i class="fa fa-check"></i><b>15.2.2</b> 连接MongoDB</a></li>
<li class="chapter" data-level="15.2.3" data-path="databases.html"><a href="databases.html#指定数据库"><i class="fa fa-check"></i><b>15.2.3</b> 指定数据库</a></li>
<li class="chapter" data-level="15.2.4" data-path="databases.html"><a href="databases.html#指定集合"><i class="fa fa-check"></i><b>15.2.4</b> 指定集合</a></li>
<li class="chapter" data-level="15.2.5" data-path="databases.html"><a href="databases.html#插入数据"><i class="fa fa-check"></i><b>15.2.5</b> 插入数据</a></li>
<li class="chapter" data-level="15.2.6" data-path="databases.html"><a href="databases.html#查询数据-1"><i class="fa fa-check"></i><b>15.2.6</b> 查询数据</a></li>
<li class="chapter" data-level="15.2.7" data-path="databases.html"><a href="databases.html#计数-1"><i class="fa fa-check"></i><b>15.2.7</b> 计数</a></li>
<li class="chapter" data-level="15.2.8" data-path="databases.html"><a href="databases.html#排序"><i class="fa fa-check"></i><b>15.2.8</b> 排序</a></li>
<li class="chapter" data-level="15.2.9" data-path="databases.html"><a href="databases.html#偏移和限定"><i class="fa fa-check"></i><b>15.2.9</b> 偏移和限定</a></li>
<li class="chapter" data-level="15.2.10" data-path="databases.html"><a href="databases.html#更新"><i class="fa fa-check"></i><b>15.2.10</b> 更新</a></li>
<li class="chapter" data-level="15.2.11" data-path="databases.html"><a href="databases.html#删除"><i class="fa fa-check"></i><b>15.2.11</b> 删除</a></li>
<li class="chapter" data-level="15.2.12" data-path="databases.html"><a href="databases.html#其他操作"><i class="fa fa-check"></i><b>15.2.12</b> 其他操作</a></li>
</ul></li>
<li class="chapter" data-level="15.3" data-path="databases.html"><a href="databases.html#参考资料-4"><i class="fa fa-check"></i><b>15.3</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="selenium.html"><a href="selenium.html"><i class="fa fa-check"></i><b>16</b> 使用Selenium抓取动态渲染页面</a><ul>
<li class="chapter" data-level="16.1" data-path="selenium.html"><a href="selenium.html#安装-1"><i class="fa fa-check"></i><b>16.1</b> 安装</a></li>
<li class="chapter" data-level="16.2" data-path="selenium.html"><a href="selenium.html#selenium的使用"><i class="fa fa-check"></i><b>16.2</b> Selenium的使用</a><ul>
<li class="chapter" data-level="16.2.1" data-path="selenium.html"><a href="selenium.html#初始化浏览器对象"><i class="fa fa-check"></i><b>16.2.1</b> 初始化浏览器对象</a></li>
<li class="chapter" data-level="16.2.2" data-path="selenium.html"><a href="selenium.html#访问页面"><i class="fa fa-check"></i><b>16.2.2</b> 访问页面</a></li>
<li class="chapter" data-level="16.2.3" data-path="selenium.html"><a href="selenium.html#查找节点"><i class="fa fa-check"></i><b>16.2.3</b> 查找节点</a></li>
<li class="chapter" data-level="16.2.4" data-path="selenium.html"><a href="selenium.html#节点操作"><i class="fa fa-check"></i><b>16.2.4</b> 节点操作</a></li>
<li class="chapter" data-level="16.2.5" data-path="selenium.html"><a href="selenium.html#执行javascript"><i class="fa fa-check"></i><b>16.2.5</b> 执行JavaScript</a></li>
<li class="chapter" data-level="16.2.6" data-path="selenium.html"><a href="selenium.html#获取节点信息"><i class="fa fa-check"></i><b>16.2.6</b> 获取节点信息</a></li>
<li class="chapter" data-level="16.2.7" data-path="selenium.html"><a href="selenium.html#延时等待"><i class="fa fa-check"></i><b>16.2.7</b> 延时等待</a></li>
</ul></li>
<li class="chapter" data-level="16.3" data-path="selenium.html"><a href="selenium.html#扩展阅读"><i class="fa fa-check"></i><b>16.3</b> 扩展阅读</a></li>
</ul></li>
<li class="part"><span><b>IV 网络应用开发</b></span></li>
<li class="chapter" data-level="17" data-path="django.html"><a href="django.html"><i class="fa fa-check"></i><b>17</b> Django 框架的安装</a><ul>
<li class="chapter" data-level="17.1" data-path="django.html"><a href="django.html#安装-2"><i class="fa fa-check"></i><b>17.1</b> 安装</a><ul>
<li class="chapter" data-level="17.1.1" data-path="django.html"><a href="django.html#安装-django"><i class="fa fa-check"></i><b>17.1.1</b> 安装 Django</a></li>
<li class="chapter" data-level="17.1.2" data-path="django.html"><a href="django.html#验证安装"><i class="fa fa-check"></i><b>17.1.2</b> 验证安装</a></li>
<li class="chapter" data-level="17.1.3" data-path="django.html"><a href="django.html#创建项目并预览效果"><i class="fa fa-check"></i><b>17.1.3</b> 创建项目并预览效果</a></li>
</ul></li>
<li class="chapter" data-level="17.2" data-path="django.html"><a href="django.html#学习资源-1"><i class="fa fa-check"></i><b>17.2</b> 学习资源</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="quicktutorial.html"><a href="quicktutorial.html"><i class="fa fa-check"></i><b>18</b> 快速上手案例</a><ul>
<li class="chapter" data-level="18.1" data-path="quicktutorial.html"><a href="quicktutorial.html#在-django-项目中创建-app"><i class="fa fa-check"></i><b>18.1</b> 在 Django 项目中创建 APP</a><ul>
<li class="chapter" data-level="18.1.1" data-path="quicktutorial.html"><a href="quicktutorial.html#初始化-app"><i class="fa fa-check"></i><b>18.1.1</b> 初始化 APP</a></li>
<li class="chapter" data-level="18.1.2" data-path="quicktutorial.html"><a href="quicktutorial.html#将应用添加到项目中"><i class="fa fa-check"></i><b>18.1.2</b> 将应用添加到项目中</a></li>
<li class="chapter" data-level="18.1.3" data-path="quicktutorial.html"><a href="quicktutorial.html#创建视图"><i class="fa fa-check"></i><b>18.1.3</b> 创建视图</a></li>
<li class="chapter" data-level="18.1.4" data-path="quicktutorial.html"><a href="quicktutorial.html#规划路由"><i class="fa fa-check"></i><b>18.1.4</b> 规划路由</a></li>
<li class="chapter" data-level="18.1.5" data-path="quicktutorial.html"><a href="quicktutorial.html#预览效果"><i class="fa fa-check"></i><b>18.1.5</b> 预览效果</a></li>
</ul></li>
<li class="chapter" data-level="18.2" data-path="quicktutorial.html"><a href="quicktutorial.html#模型的创建及使用"><i class="fa fa-check"></i><b>18.2</b> 模型的创建及使用</a><ul>
<li class="chapter" data-level="18.2.1" data-path="quicktutorial.html"><a href="quicktutorial.html#配置项目信息"><i class="fa fa-check"></i><b>18.2.1</b> 配置项目信息</a></li>
<li class="chapter" data-level="18.2.2" data-path="quicktutorial.html"><a href="quicktutorial.html#创建模型"><i class="fa fa-check"></i><b>18.2.2</b> 创建模型</a></li>
<li class="chapter" data-level="18.2.3" data-path="quicktutorial.html"><a href="quicktutorial.html#激活模型"><i class="fa fa-check"></i><b>18.2.3</b> 激活模型</a></li>
<li class="chapter" data-level="18.2.4" data-path="quicktutorial.html"><a href="quicktutorial.html#数据库的生成与迁移"><i class="fa fa-check"></i><b>18.2.4</b> 数据库的生成与迁移</a></li>
</ul></li>
<li class="chapter" data-level="18.3" data-path="quicktutorial.html"><a href="quicktutorial.html#后台管理"><i class="fa fa-check"></i><b>18.3</b> 后台管理</a><ul>
<li class="chapter" data-level="18.3.1" data-path="quicktutorial.html"><a href="quicktutorial.html#创建管理员账号"><i class="fa fa-check"></i><b>18.3.1</b> 创建管理员账号</a></li>
<li class="chapter" data-level="18.3.2" data-path="quicktutorial.html"><a href="quicktutorial.html#添加引用到后台管理"><i class="fa fa-check"></i><b>18.3.2</b> 添加引用到后台管理</a></li>
<li class="chapter" data-level="18.3.3" data-path="quicktutorial.html"><a href="quicktutorial.html#进入后台管理界面"><i class="fa fa-check"></i><b>18.3.3</b> 进入后台管理界面</a></li>
</ul></li>
<li class="chapter" data-level="18.4" data-path="quicktutorial.html"><a href="quicktutorial.html#创建视图-1"><i class="fa fa-check"></i><b>18.4</b> 创建视图</a><ul>
<li class="chapter" data-level="18.4.1" data-path="quicktutorial.html"><a href="quicktutorial.html#添加多个视图"><i class="fa fa-check"></i><b>18.4.1</b> 添加多个视图</a></li>
<li class="chapter" data-level="18.4.2" data-path="quicktutorial.html"><a href="quicktutorial.html#模版命名空间"><i class="fa fa-check"></i><b>18.4.2</b> 模版命名空间</a></li>
<li class="chapter" data-level="18.4.3" data-path="quicktutorial.html"><a href="quicktutorial.html#在视图中使用模板"><i class="fa fa-check"></i><b>18.4.3</b> 在视图中使用模板</a></li>
<li class="chapter" data-level="18.4.4" data-path="quicktutorial.html"><a href="quicktutorial.html#url命名空间"><i class="fa fa-check"></i><b>18.4.4</b> URL命名空间</a></li>
</ul></li>
<li class="chapter" data-level="18.5" data-path="quicktutorial.html"><a href="quicktutorial.html#模型"><i class="fa fa-check"></i><b>18.5</b> 模型</a><ul>
<li class="chapter" data-level="18.5.1" data-path="quicktutorial.html"><a href="quicktutorial.html#设置数据库信息-1"><i class="fa fa-check"></i><b>18.5.1</b> 设置数据库信息</a></li>
<li class="chapter" data-level="18.5.2" data-path="quicktutorial.html"><a href="quicktutorial.html#创建模型-1"><i class="fa fa-check"></i><b>18.5.2</b> 创建模型</a></li>
<li class="chapter" data-level="18.5.3" data-path="quicktutorial.html"><a href="quicktutorial.html#使用模型"><i class="fa fa-check"></i><b>18.5.3</b> 使用模型</a></li>
</ul></li>
<li class="chapter" data-level="18.6" data-path="quicktutorial.html"><a href="quicktutorial.html#参考文献-1"><i class="fa fa-check"></i><b>18.6</b> 参考文献</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://bookdown.org" target="blank">本书由 bookdown 强力驱动</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Python 编程</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="decorator" class="section level1">
<h1><span class="header-section-number">第 5 章</span> Python 装饰器</h1>
<p>装饰器本质上是一个 Python 函数或类，它可以让其他函数或类在不需要做任何代码修改的前提下增加额外功能，装饰器的返回值也是一个函数/类对象。有了装饰器，我们就可以抽离出大量与函数功能本身无关的雷同代码到装饰器中并继续重用。概括的讲，装饰器的作用就是为已经存在的对象添加额外的功能。</p>
<div id="为什么要用装饰器" class="section level2">
<h2><span class="header-section-number">5.1</span> 为什么要用装饰器</h2>
<p>先来看一个简单例子，虽然实际代码可能比这复杂很多，假如已经有如下函数，用来输出特定信息，并且这个函数已被其他程序片段使用多次：</p>
<div class="sourceCode" id="cb153"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb153-1"><a href="decorator.html#cb153-1"></a><span class="kw">def</span> foo():</span>
<span id="cb153-2"><a href="decorator.html#cb153-2"></a>    <span class="bu">print</span>(<span class="st">&#39;i am foo&#39;</span>)</span></code></pre></div>
<p>现在有一个新的需求，希望可以记录下函数的执行日志，于是在代码中添加日志代码：</p>
<div class="sourceCode" id="cb154"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb154-1"><a href="decorator.html#cb154-1"></a><span class="kw">def</span> foo():</span>
<span id="cb154-2"><a href="decorator.html#cb154-2"></a>    <span class="bu">print</span>(<span class="st">&#39;i am foo&#39;</span>)</span>
<span id="cb154-3"><a href="decorator.html#cb154-3"></a>    logging.info(<span class="st">&quot;foo is running&quot;</span>)</span></code></pre></div>
<p>如果函数 bar()、bar2() 也有类似的需求，怎么做？再写一个 logging 在 bar 函数里？这样就造成大量雷同的代码，为了减少重复写代码，我们可以这样做，重新定义一个新的函数，专门处理日志 ，日志处理完之后再执行真正的业务代码：</p>
<div class="sourceCode" id="cb155"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb155-1"><a href="decorator.html#cb155-1"></a><span class="kw">def</span> use_logging(func):</span>
<span id="cb155-2"><a href="decorator.html#cb155-2"></a>    logging.warn(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> is running&quot;</span> <span class="op">%</span> func.<span class="va">__name__</span>)</span>
<span id="cb155-3"><a href="decorator.html#cb155-3"></a>    func()</span>
<span id="cb155-4"><a href="decorator.html#cb155-4"></a></span>
<span id="cb155-5"><a href="decorator.html#cb155-5"></a><span class="kw">def</span> foo():</span>
<span id="cb155-6"><a href="decorator.html#cb155-6"></a>    <span class="bu">print</span>(<span class="st">&#39;i am foo&#39;</span>)</span>
<span id="cb155-7"><a href="decorator.html#cb155-7"></a></span>
<span id="cb155-8"><a href="decorator.html#cb155-8"></a>use_logging(foo)</span></code></pre></div>
<p>这样做逻辑上是没问题的，功能是实现了，但是我们调用的时候不再是调用真正的业务逻辑 foo 函数，而是换成了 use_logging 函数，这就破坏了原有的代码结构，现在我们不得不每次都要把原来的那个 foo 函数作为参数传递给 use_logging 函数，那么有没有更好的方式的呢？当然有，答案就是装饰器。</p>
</div>
<div id="简单装饰器" class="section level2">
<h2><span class="header-section-number">5.2</span> 简单装饰器</h2>
<div class="sourceCode" id="cb156"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb156-1"><a href="decorator.html#cb156-1"></a><span class="kw">def</span> use_logging(func):</span>
<span id="cb156-2"><a href="decorator.html#cb156-2"></a></span>
<span id="cb156-3"><a href="decorator.html#cb156-3"></a>    <span class="kw">def</span> wrapper():</span>
<span id="cb156-4"><a href="decorator.html#cb156-4"></a>        logging.warn(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> is running&quot;</span> <span class="op">%</span> func.<span class="va">__name__</span>)</span>
<span id="cb156-5"><a href="decorator.html#cb156-5"></a>        <span class="cf">return</span> func()   <span class="co"># 把 foo 当做参数传递进来时，执行func()就相当于执行foo()</span></span>
<span id="cb156-6"><a href="decorator.html#cb156-6"></a>    <span class="cf">return</span> wrapper</span>
<span id="cb156-7"><a href="decorator.html#cb156-7"></a></span>
<span id="cb156-8"><a href="decorator.html#cb156-8"></a><span class="kw">def</span> foo():</span>
<span id="cb156-9"><a href="decorator.html#cb156-9"></a>    <span class="bu">print</span>(<span class="st">&#39;i am foo&#39;</span>)</span>
<span id="cb156-10"><a href="decorator.html#cb156-10"></a></span>
<span id="cb156-11"><a href="decorator.html#cb156-11"></a>foo <span class="op">=</span> use_logging(foo)  <span class="co"># 因为装饰器 use_logging(foo) 返回的时函数对象 wrapper，这条语句相当于  foo = wrapper</span></span>
<span id="cb156-12"><a href="decorator.html#cb156-12"></a>foo()                   <span class="co"># 执行foo()就相当于执行 wrapper()</span></span></code></pre></div>
<p><code>use_logging()</code> 就是一个装饰器，它一个普通的函数，它把执行真正业务逻辑的函数 func 包裹在其中，看起来像 foo 被 use_logging 装饰了一样，use_logging 返回的也是一个函数，这个函数的名字叫 wrapper。在这个例子中，函数进入和退出时，被称为一个横切面，这种编程方式被称为面向切面的编程。</p>
</div>
<div id="语法糖" class="section level2">
<h2><span class="header-section-number">5.3</span> @ 语法糖</h2>
<p>如果你接触 Python 有一段时间了的话，想必你对 <code>@</code> 符号一定不陌生了，没错 <code>@</code> 符号就是装饰器的语法糖，它放在函数开始定义的地方，这样就可以省略最后一步再次赋值的操作。</p>
<div class="sourceCode" id="cb157"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb157-1"><a href="decorator.html#cb157-1"></a><span class="kw">def</span> use_logging(func):</span>
<span id="cb157-2"><a href="decorator.html#cb157-2"></a></span>
<span id="cb157-3"><a href="decorator.html#cb157-3"></a>    <span class="kw">def</span> wrapper():</span>
<span id="cb157-4"><a href="decorator.html#cb157-4"></a>        logging.warn(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> is running&quot;</span> <span class="op">%</span> func.<span class="va">__name__</span>)</span>
<span id="cb157-5"><a href="decorator.html#cb157-5"></a>        <span class="cf">return</span> func()</span>
<span id="cb157-6"><a href="decorator.html#cb157-6"></a>    <span class="cf">return</span> wrapper</span>
<span id="cb157-7"><a href="decorator.html#cb157-7"></a></span>
<span id="cb157-8"><a href="decorator.html#cb157-8"></a><span class="at">@use_logging</span></span>
<span id="cb157-9"><a href="decorator.html#cb157-9"></a><span class="kw">def</span> foo():</span>
<span id="cb157-10"><a href="decorator.html#cb157-10"></a>    <span class="bu">print</span>(<span class="st">&quot;i am foo&quot;</span>)</span>
<span id="cb157-11"><a href="decorator.html#cb157-11"></a></span>
<span id="cb157-12"><a href="decorator.html#cb157-12"></a>foo()</span></code></pre></div>
<p>如上所示，有了 <code>@</code> ，我们就可以省去<code>foo = use_logging(foo)</code>这一句了，直接调用 <code>foo()</code> 即可得到想要的结果。你们看到了没有，<code>foo()</code> 函数不需要做任何修改，只需在定义的地方加上装饰器，调用的时候还是和以前一样，如果我们有其他的类似函数，我们可以继续调用装饰器来修饰函数，而不用重复修改函数或者增加新的封装。这样，我们就提高了程序的可重复利用性，并增加了程序的可读性。</p>
<p>装饰器在 Python 使用如此方便都要归因于 Python 的函数能像普通的对象一样能作为参数传递给其他函数，可以被赋值给其他变量，可以作为返回值，可以被定义在另外一个函数内。</p>
</div>
<div id="argskwargs" class="section level2">
<h2><span class="header-section-number">5.4</span> <code>*args</code>、<code>**kwargs</code></h2>
<p>可能有人问，如果我的业务逻辑函数 foo 需要参数怎么办？比如：</p>
<div class="sourceCode" id="cb158"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb158-1"><a href="decorator.html#cb158-1"></a><span class="kw">def</span> foo(name):</span>
<span id="cb158-2"><a href="decorator.html#cb158-2"></a>    <span class="bu">print</span>(<span class="st">&quot;i am </span><span class="sc">%s</span><span class="st">&quot;</span> <span class="op">%</span> name)</span></code></pre></div>
<p>我们可以在定义 wrapper 函数的时候指定参数：</p>
<div class="sourceCode" id="cb159"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb159-1"><a href="decorator.html#cb159-1"></a><span class="kw">def</span> wrapper(name):</span>
<span id="cb159-2"><a href="decorator.html#cb159-2"></a>        logging.warn(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> is running&quot;</span> <span class="op">%</span> func.<span class="va">__name__</span>)</span>
<span id="cb159-3"><a href="decorator.html#cb159-3"></a>        <span class="cf">return</span> func(name)</span>
<span id="cb159-4"><a href="decorator.html#cb159-4"></a>    <span class="cf">return</span> wrapper</span></code></pre></div>
<p>这样 foo 函数定义的参数就可以定义在 wrapper 函数中。这时，又有人要问了，如果 foo 函数接收两个参数呢？三个参数呢？更有甚者，我可能传很多个。当装饰器不知道 foo 到底有多少个参数时，我们可以用 *args 来代替：</p>
<div class="sourceCode" id="cb160"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb160-1"><a href="decorator.html#cb160-1"></a><span class="kw">def</span> wrapper(<span class="op">*</span>args):</span>
<span id="cb160-2"><a href="decorator.html#cb160-2"></a>        logging.warn(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> is running&quot;</span> <span class="op">%</span> func.<span class="va">__name__</span>)</span>
<span id="cb160-3"><a href="decorator.html#cb160-3"></a>        <span class="cf">return</span> func(<span class="op">*</span>args)</span>
<span id="cb160-4"><a href="decorator.html#cb160-4"></a>    <span class="cf">return</span> wrapper</span></code></pre></div>
<p>如此一来，甭管 foo 定义了多少个参数，我都可以完整地传递到 func 中去。这样就不影响 foo 的业务逻辑了。这时还有读者会问，如果 foo 函数还定义了一些关键字参数呢？比如：</p>
<div class="sourceCode" id="cb161"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb161-1"><a href="decorator.html#cb161-1"></a><span class="kw">def</span> foo(name, age<span class="op">=</span><span class="va">None</span>, height<span class="op">=</span><span class="va">None</span>):</span>
<span id="cb161-2"><a href="decorator.html#cb161-2"></a>    <span class="bu">print</span>(<span class="st">&quot;I am </span><span class="sc">%s</span><span class="st">, age </span><span class="sc">%s</span><span class="st">, height </span><span class="sc">%s</span><span class="st">&quot;</span> <span class="op">%</span> (name, age, height))</span></code></pre></div>
<p>这时，你就可以把 wrapper 函数指定关键字函数：</p>
<div class="sourceCode" id="cb162"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb162-1"><a href="decorator.html#cb162-1"></a><span class="kw">def</span> wrapper(<span class="op">*</span>args, <span class="op">**</span>kwargs):</span>
<span id="cb162-2"><a href="decorator.html#cb162-2"></a>        <span class="co"># args是一个数组，kwargs一个字典</span></span>
<span id="cb162-3"><a href="decorator.html#cb162-3"></a>        logging.warn(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> is running&quot;</span> <span class="op">%</span> func.<span class="va">__name__</span>)</span>
<span id="cb162-4"><a href="decorator.html#cb162-4"></a>        <span class="cf">return</span> func(<span class="op">*</span>args, <span class="op">**</span>kwargs)</span>
<span id="cb162-5"><a href="decorator.html#cb162-5"></a>    <span class="cf">return</span> wrapper</span></code></pre></div>
</div>
<div id="带参数的装饰器" class="section level2">
<h2><span class="header-section-number">5.5</span> 带参数的装饰器</h2>
<p>装饰器还有更大的灵活性，例如带参数的装饰器，在上面的装饰器调用中，该装饰器接收唯一的参数就是执行业务的函数 foo 。装饰器的语法允许我们在调用时，提供其它参数，比如<code>@decorator(a)</code>。这样，就为装饰器的编写和使用提供了更大的灵活性。比如，我们可以在装饰器中指定日志的等级，因为不同业务函数可能需要的日志级别是不一样的。</p>
<div class="sourceCode" id="cb163"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb163-1"><a href="decorator.html#cb163-1"></a><span class="kw">def</span> use_logging(level):</span>
<span id="cb163-2"><a href="decorator.html#cb163-2"></a>    <span class="kw">def</span> decorator(func):</span>
<span id="cb163-3"><a href="decorator.html#cb163-3"></a>        <span class="kw">def</span> wrapper(<span class="op">*</span>args, <span class="op">**</span>kwargs):</span>
<span id="cb163-4"><a href="decorator.html#cb163-4"></a>            <span class="cf">if</span> level <span class="op">==</span> <span class="st">&quot;warn&quot;</span>:</span>
<span id="cb163-5"><a href="decorator.html#cb163-5"></a>                logging.warn(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> is running&quot;</span> <span class="op">%</span> func.<span class="va">__name__</span>)</span>
<span id="cb163-6"><a href="decorator.html#cb163-6"></a>            <span class="cf">elif</span> level <span class="op">==</span> <span class="st">&quot;info&quot;</span>:</span>
<span id="cb163-7"><a href="decorator.html#cb163-7"></a>                logging.info(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> is running&quot;</span> <span class="op">%</span> func.<span class="va">__name__</span>)</span>
<span id="cb163-8"><a href="decorator.html#cb163-8"></a>            <span class="cf">return</span> func(<span class="op">*</span>args)</span>
<span id="cb163-9"><a href="decorator.html#cb163-9"></a>        <span class="cf">return</span> wrapper</span>
<span id="cb163-10"><a href="decorator.html#cb163-10"></a></span>
<span id="cb163-11"><a href="decorator.html#cb163-11"></a>    <span class="cf">return</span> decorator</span>
<span id="cb163-12"><a href="decorator.html#cb163-12"></a></span>
<span id="cb163-13"><a href="decorator.html#cb163-13"></a><span class="at">@use_logging</span>(level<span class="op">=</span><span class="st">&quot;warn&quot;</span>)</span>
<span id="cb163-14"><a href="decorator.html#cb163-14"></a><span class="kw">def</span> foo(name<span class="op">=</span><span class="st">&#39;foo&#39;</span>):</span>
<span id="cb163-15"><a href="decorator.html#cb163-15"></a>    <span class="bu">print</span>(<span class="st">&quot;i am </span><span class="sc">%s</span><span class="st">&quot;</span> <span class="op">%</span> name)</span>
<span id="cb163-16"><a href="decorator.html#cb163-16"></a></span>
<span id="cb163-17"><a href="decorator.html#cb163-17"></a>foo()</span></code></pre></div>
<p>上面的 use_logging 是允许带参数的装饰器。它实际上是对原有装饰器的一个函数封装，并返回一个装饰器。我们可以将它理解为一个含有参数的闭包。当我 们使用<code>@use_logging(level="warn")</code>调用的时候，Python 能够发现这一层的封装，并把参数传递到装饰器的环境中。</p>
<p><code>@use_logging(level="warn")</code>等价于<code>@decorator</code></p>
</div>
<div id="类装饰器" class="section level2">
<h2><span class="header-section-number">5.6</span> 类装饰器</h2>
<p>没错，装饰器不仅可以是函数，还可以是类，相比函数装饰器，类装饰器具有灵活度大、高内聚、封装性等优点。使用类装饰器主要依靠类的<code>__call__</code>方法，当使用 <code>@</code> 形式将装饰器附加到函数上时，就会调用此方法。</p>
<div class="sourceCode" id="cb164"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb164-1"><a href="decorator.html#cb164-1"></a><span class="kw">class</span> Foo(<span class="bu">object</span>):</span>
<span id="cb164-2"><a href="decorator.html#cb164-2"></a>    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, func):</span>
<span id="cb164-3"><a href="decorator.html#cb164-3"></a>        <span class="va">self</span>._func <span class="op">=</span> func</span>
<span id="cb164-4"><a href="decorator.html#cb164-4"></a></span>
<span id="cb164-5"><a href="decorator.html#cb164-5"></a>    <span class="kw">def</span> <span class="fu">__call__</span>(<span class="va">self</span>):</span>
<span id="cb164-6"><a href="decorator.html#cb164-6"></a>        <span class="bu">print</span> (<span class="st">&#39;class decorator runing&#39;</span>)</span>
<span id="cb164-7"><a href="decorator.html#cb164-7"></a>        <span class="va">self</span>._func()</span>
<span id="cb164-8"><a href="decorator.html#cb164-8"></a>        <span class="bu">print</span> (<span class="st">&#39;class decorator ending&#39;</span>)</span>
<span id="cb164-9"><a href="decorator.html#cb164-9"></a></span>
<span id="cb164-10"><a href="decorator.html#cb164-10"></a><span class="at">@Foo</span></span>
<span id="cb164-11"><a href="decorator.html#cb164-11"></a><span class="kw">def</span> bar():</span>
<span id="cb164-12"><a href="decorator.html#cb164-12"></a>    <span class="bu">print</span> (<span class="st">&#39;bar&#39;</span>)</span>
<span id="cb164-13"><a href="decorator.html#cb164-13"></a></span>
<span id="cb164-14"><a href="decorator.html#cb164-14"></a>bar()</span></code></pre></div>
</div>
<div id="functools.wraps" class="section level2">
<h2><span class="header-section-number">5.7</span> functools.wraps</h2>
<p>使用装饰器极大地复用了代码，但是他有一个缺点就是原函数的元信息不见了，比如函数的<code>docstring</code>、<code>__name__</code>、参数列表，先看例子：</p>
<div class="sourceCode" id="cb165"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb165-1"><a href="decorator.html#cb165-1"></a><span class="co">#装饰器</span></span>
<span id="cb165-2"><a href="decorator.html#cb165-2"></a><span class="kw">def</span> logged(func):</span>
<span id="cb165-3"><a href="decorator.html#cb165-3"></a>    <span class="kw">def</span> with_logging(<span class="op">*</span>args, <span class="op">**</span>kwargs):</span>
<span id="cb165-4"><a href="decorator.html#cb165-4"></a>        <span class="bu">print</span> func.<span class="va">__name__</span>      <span class="co"># 输出 &#39;with_logging&#39;</span></span>
<span id="cb165-5"><a href="decorator.html#cb165-5"></a>        <span class="bu">print</span> func.__doc__       <span class="co"># 输出 None</span></span>
<span id="cb165-6"><a href="decorator.html#cb165-6"></a>        <span class="cf">return</span> func(<span class="op">*</span>args, <span class="op">**</span>kwargs)</span>
<span id="cb165-7"><a href="decorator.html#cb165-7"></a>    <span class="cf">return</span> with_logging</span>
<span id="cb165-8"><a href="decorator.html#cb165-8"></a></span>
<span id="cb165-9"><a href="decorator.html#cb165-9"></a><span class="co"># 函数</span></span>
<span id="cb165-10"><a href="decorator.html#cb165-10"></a><span class="at">@logged</span></span>
<span id="cb165-11"><a href="decorator.html#cb165-11"></a><span class="kw">def</span> f(x):</span>
<span id="cb165-12"><a href="decorator.html#cb165-12"></a>   <span class="co">&quot;&quot;&quot;does some math&quot;&quot;&quot;</span></span>
<span id="cb165-13"><a href="decorator.html#cb165-13"></a>   <span class="cf">return</span> x <span class="op">+</span> x <span class="op">*</span> x</span>
<span id="cb165-14"><a href="decorator.html#cb165-14"></a></span>
<span id="cb165-15"><a href="decorator.html#cb165-15"></a>logged(f)</span></code></pre></div>
<p>不难发现，函数 <code>f</code> 被<code>with_logging</code>取代了，当然它的<code>docstring</code>，<code>__name__</code>就是变成了<code>with_logging</code>函数的信息了。好在我们有<code>functools.wraps</code>，<code>wraps</code>本身也是一个装饰器，它能把原函数的元信息拷贝到装饰器里面的 <code>func</code> 函数中，这使得装饰器里面的 <code>func</code> 函数也有和原函数 foo 一样的元信息了。</p>
<div class="sourceCode" id="cb166"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb166-1"><a href="decorator.html#cb166-1"></a><span class="im">from</span> functools <span class="im">import</span> wraps</span>
<span id="cb166-2"><a href="decorator.html#cb166-2"></a><span class="kw">def</span> logged(func):</span>
<span id="cb166-3"><a href="decorator.html#cb166-3"></a>    <span class="at">@wraps</span>(func)</span>
<span id="cb166-4"><a href="decorator.html#cb166-4"></a>    <span class="kw">def</span> with_logging(<span class="op">*</span>args, <span class="op">**</span>kwargs):</span>
<span id="cb166-5"><a href="decorator.html#cb166-5"></a>        <span class="bu">print</span> func.<span class="va">__name__</span>      <span class="co"># 输出 &#39;f&#39;</span></span>
<span id="cb166-6"><a href="decorator.html#cb166-6"></a>        <span class="bu">print</span> func.__doc__       <span class="co"># 输出 &#39;does some math&#39;</span></span>
<span id="cb166-7"><a href="decorator.html#cb166-7"></a>        <span class="cf">return</span> func(<span class="op">*</span>args, <span class="op">**</span>kwargs)</span>
<span id="cb166-8"><a href="decorator.html#cb166-8"></a>    <span class="cf">return</span> with_logging</span>
<span id="cb166-9"><a href="decorator.html#cb166-9"></a></span>
<span id="cb166-10"><a href="decorator.html#cb166-10"></a><span class="at">@logged</span></span>
<span id="cb166-11"><a href="decorator.html#cb166-11"></a><span class="kw">def</span> f(x):</span>
<span id="cb166-12"><a href="decorator.html#cb166-12"></a>   <span class="co">&quot;&quot;&quot;does some math&quot;&quot;&quot;</span></span>
<span id="cb166-13"><a href="decorator.html#cb166-13"></a>   <span class="cf">return</span> x <span class="op">+</span> x <span class="op">*</span> x</span></code></pre></div>
</div>
<div id="装饰器顺序" class="section level2">
<h2><span class="header-section-number">5.8</span> 装饰器顺序</h2>
<p>一个函数还可以同时定义多个装饰器，比如：</p>
<div class="sourceCode" id="cb167"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb167-1"><a href="decorator.html#cb167-1"></a><span class="at">@a</span></span>
<span id="cb167-2"><a href="decorator.html#cb167-2"></a><span class="at">@b</span></span>
<span id="cb167-3"><a href="decorator.html#cb167-3"></a><span class="at">@c</span></span>
<span id="cb167-4"><a href="decorator.html#cb167-4"></a><span class="kw">def</span> f ():</span>
<span id="cb167-5"><a href="decorator.html#cb167-5"></a>    <span class="cf">pass</span></span></code></pre></div>
<p>它的执行顺序是从里到外，最先调用最里层的装饰器，最后调用最外层的装饰器，它等效于</p>
<div class="sourceCode" id="cb168"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb168-1"><a href="decorator.html#cb168-1"></a>f <span class="op">=</span> a(b(c(f)))</span></code></pre></div>
</div>
<div id="参考资料-1" class="section level2 unnumbered">
<h2>参考资料</h2>
<ol style="list-style-type: decimal">
<li><a href="https://foofish.net/python-decorator.html">理解 Python 装饰器</a></li>
</ol>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="exception.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="module.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/yangjh-xbmu/learningpython/edit/master/0500-syntax-decorator.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["learningpython.pdf", "learningpython.epub"],
"toc": {
"collapse": "section"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
